home *** CD-ROM | disk | FTP | other *** search
/ NetNews Offline 2 / NetNews Offline Volume 2.iso / news / comp / lang / c-part1 / 3924 < prev    next >
Encoding:
Text File  |  1996-08-05  |  2.9 KB  |  121 lines

  1. Path: newsbf02.news.aol.com!not-for-mail
  2. From: agent439@aol.com (Agent439)
  3. Newsgroups: comp.lang.c
  4. Subject: Multiplication Problem - How to multiply really big numbers, and printing the answers out.
  5. Date: 31 Jan 1996 19:07:18 -0500
  6. Organization: America Online, Inc. (1-800-827-6364)
  7. Sender: root@newsbf02.news.aol.com
  8. Message-ID: <4ep07m$4fn@newsbf02.news.aol.com>
  9. Reply-To: agent439@aol.com (Agent439)
  10. NNTP-Posting-Host: newsbf02.mail.aol.com
  11.  
  12. I'm trying to write a C Program that will accept a positive integer that
  13. can be 100 digits long at a maximum.  So far, my first example (INPUT
  14. 3125, 25) works except that when it tries to print out the answer, the
  15. number is wrong.  Now I know there are 2 warnings in it, but I watched
  16. variables step-by-step, and it seems to get the right answer.  Please
  17. disregard the timer part, and any disagreements what I call my variables. 
  18. Just tell me what types should my variables be or what printf conversion
  19. should be in for the answer?  Any suggestions to get rid of the warnings
  20. would be appreciated too. :-0
  21.  
  22. Here is the program
  23. #include <math.h>
  24. #include <stdio.h>
  25. #include <time.h>
  26.  
  27. #define FALSE 0
  28. #define TRUE !FALSE
  29.  
  30. float timer(int reset);
  31. void main(){
  32.     
  33.     char a[100],b[100];
  34.     int i, j;
  35.     int x, y, z;
  36.     int size,truesize;
  37.     unsigned long pa,pb, c;
  38.     
  39.     a==NULL;
  40.     b==NULL;
  41.     printf("Enter the Multiplicand:");
  42.     gets(a);
  43.     while(a<0){
  44.         printf("Positive Integers only please!");
  45.         printf("Enter the Multiplicand:");
  46.         gets(a);
  47.     }
  48.     
  49.     printf("Enter the Multiplier:");
  50.     gets(b);
  51.     while(b<0){
  52.         printf("Positive Integers only please!");
  53.         printf("Enter the Multiplier:");
  54.         gets(b);
  55.     }
  56.     size=0;
  57.     pa=0;
  58.     while(a[size] != 0){
  59.     size++;
  60.     }
  61.     size--;
  62.     truesize=size;
  63.     for(size==size;size>=0;size--){
  64.     pa += (a[size]-48)*(pow(10,(truesize-size))); //change char to integer
  65. value 
  66.         }
  67.     size=0;
  68.     pb=0;
  69.     while(b[size] != 0){
  70.     size++;
  71.     }
  72.     size--;
  73.     truesize=size;
  74.     for(size==size;size>=0;size--){
  75.     pb += (b[size]-48)*(pow(10,(truesize-size))); //change char to
  76. integer value
  77.         }
  78.     //timer(TRUE);
  79.     c=0;
  80.     c+=(pa);//Start C with the amount in pa
  81.     c*=(pb);    //Multiply pa by the integer value of pb
  82.     printf("The product is =======> %i \n",c);
  83.     for(i=0;i<1000;i++){
  84.         for(j=0;j<1000;j++){
  85.             z= x++ + ++y;
  86.         }
  87.     }
  88.     printf("Elapsed time = %.2f seconds\n", timer(FALSE));
  89.     
  90.     timer(TRUE);
  91.     for(i = 0; i < 1000; i++){
  92.         for (j = 0; j < 1000; j++){
  93.             y++;
  94.             z = x + y;
  95.             x++;
  96.         }
  97.     }
  98.     printf("Elapsed time = %.2f seconds\n", timer(FALSE));
  99. }    
  100.     
  101.  
  102.  
  103. float timer(int reset){
  104.     static clock_t start;
  105.     float elapsed;
  106.     
  107.     elapsed = (float)(clock() - start) / CLOCKS_PER_SEC;
  108.     if(reset){
  109.         start = clock();
  110.     }
  111.     return(elapsed);
  112. }
  113. Agent439@aol.com
  114. Fan of Doom, Doom2----Let the obsession begin....Again! (and again, and
  115. again!)
  116. Fan of Simpsons-----'nuff said
  117. Fan of Bulls-----Jordan's back! :-)
  118. Fan of AOL------3,000,000 members!  (Only 93,000,000 households to go!)
  119. Fan of Van Halen
  120. Fan of Mancow's Morning Madhouse-ROCK 103.5FM (Chicago,Illinois)
  121.